package com.tianxiabuyi.weixin.qy.contact;

import com.alibaba.fastjson.annotation.JSONField;
import com.tianxiabuyi.utils.JSONUtil;
import com.tianxiabuyi.weixin.common.WxClient;
import com.tianxiabuyi.weixin.qy.QyWxClientFactory;
import com.tianxiabuyi.weixin.qy.contact.department.Department;

import java.util.List;

/**
 * 部门管理
 */
public class Departments {


    private WxClient wxClient;

    /**
     * 初始化wxclient
     * @param corpId
     * @param corpSecret
     * @return
     */
    public static Departments with(String corpId, String corpSecret) {
        Departments departments = new Departments();
        departments.setWxClient(QyWxClientFactory.getInstance().with(corpId, corpSecret));
        return departments;
    }

    /**
     * 设置wxclient，如果已使用with初始化，则不用调用该方法
     * @param wxClient
     */
    public void setWxClient(WxClient wxClient) {
        this.wxClient = wxClient;
    }

    /**
     * 根据id获取部门信息
     * @param id
     * @return
     */
    public Department get(int id) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/get?id=%s";
        String user = wxClient.get(String.format(url, id));
        return JSONUtil.fromJson(user, Department.class);
    }

    /**
     * 创建部门
     * @param department
     */
    public void create(Department department) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create";
        String json = JSONUtil.toJson(department);
        wxClient.post(url, json);
    }

    /**
     * 更新部门
     * @param department
     */
    public void update(Department department) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/update";
        String json = JSONUtil.toJson(department);
        wxClient.post(url, json);
    }

    /**
     * 根据id删除部门
     * @param id
     */
    public void delete(int id) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=%s";
        wxClient.get(String.format(url, id));
    }

    /**
     * 获取企业号下所有部门
     * @return
     */
    public List<Department> list() {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
        String response = wxClient.get(url);
        DepartmentList departmentList = JSONUtil.fromJson(response, DepartmentList.class);
        return departmentList.getDepartments();
    }

    /**
     * 根据id获取该部门及子部门
     * @param id
     * @return
     */
    public List<Department> list(int id) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?id=%s";
        String response = wxClient.get(String.format(url, id));
        DepartmentList departmentList = JSONUtil.fromJson(response, DepartmentList.class);
        return departmentList.getDepartments();
    }

    /**
     * 部门列表
     */
    public static class DepartmentList {
        @JSONField(name = "department")
        private List<Department> departments;


        public List<Department> getDepartments() {
            return departments;
        }

        public void setDepartments(List<Department> departments) {
            this.departments = departments;
        }
    }
}
